<template>
    <div class="app-container">
        <div class="search-container">
            <el-form ref="formRef" :inline="true" :model="form" :rules="rules" label-width="100px">
                <el-row>
                    <el-col :span="6">
                        <el-form-item label="住院号" prop="hospitalNo">
                            <el-input v-model="form.hospitalNo" :disabled="true"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="7">
                        <el-form-item label="患者信息" prop="name">
                            <span v-if="!form.visitId">暂无患者</span>
                            <span v-else>
                                <span class="fz15 mr5" style="color: #7171ff">{{
                                        form.name
                                    }}</span>
                                <span class="fz13 mr5">{{ form.age }} 岁{{ form.ageMonth }} 月{{
                                        form.ageDay
                                    }}天{{
                                        form.ageHours
                                    }}小时</span>

                                <svg-icon :icon-class="dictStore.getMateDictInfo(dictDataMap['sys_user_sex'], form.sex)
                                    .dictValue === '1'
                                    ? 'man'
                                    : 'woman'
                                    "/>
                            </span>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="身份证号" prop="idNo">
                            <el-input v-model="form.idNo" :disabled="true"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="入院科室" prop="deptAdmissionToName">
                            <el-input v-model="form.deptAdmissionToName" :disabled="true"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="7">
                        <el-form-item label="发病时间" prop="situationTime">
                            <el-input v-model="form.situationTime" :disabled="true"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="入院情况" prop="hospitalSituation">
                            <dict-data-tag :options="dictDataMap['admission_situation_dict']"
                                           :value="form.hospitalSituation"/>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-divider/>
                <el-row>
                    <el-col :span="6">
                        <el-form-item label="管床医生" prop="doctorInCharge">
                            <el-select v-model="form.doctorInCharge" :disabled="!form.visitId" placeholder="请选择"
                                       style="width: 120px">
                                <el-option v-for="item in orgVsUserList" :key="item.userId" :label="item.nickName"
                                           :value="item.userId"/>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="7">
                        <el-form-item label="上级医生" prop="upperDoctor">
                            <el-select v-model="form.upperDoctor" :disabled="!form.visitId" placeholder="请选择"
                                       style="width: 120px">
                                <el-option v-for="item in orgVsUserList" :key="item.userId" :label="item.nickName"
                                           :value="item.userId"/>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="主任医生" prop="directorDoctor">
                            <el-select v-model="form.directorDoctor" :disabled="!form.visitId" placeholder="请选择"
                                       style="width: 120px">
                                <el-option v-for="item in orgVsUserList" :key="item.userId" :label="item.nickName"
                                           :value="item.userId"/>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="5">
                        <el-form-item label="质控医生" prop="qualityDoctor">
                            <el-select v-model="form.qualityDoctor" :disabled="!form.visitId" placeholder="请选择"
                                       style="width: 120px">
                                <el-option v-for="item in orgVsUserList" :key="item.userId" :label="item.nickName"
                                           :value="item.userId"/>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-button class="mb5 mr5" style="float: right;" type="primary"
                                   @click="submitForm">移入科室确认
                        </el-button>
                    </el-col>
                </el-row>
            </el-form>
        </div>
        <el-row>待入科室患者列表</el-row>
        <el-form ref="queryParamsRef" :inline="true" :model="queryParams" label-width="100px">
            <el-form-item label="入院登记时间" prop="admissionTime">
                <el-date-picker v-model="dateRange" :unlink-panels="true" end-placeholder="结束日期"
                                range-separator="截止"
                                start-placeholder="开始日期" type="datetimerange" value-format="YYYY-MM-DD HH:mm:ss"
                                @change="handleQuery"></el-date-picker>
            </el-form-item>
            <el-form-item label="患者名称" prop="name">
                <el-input v-model="queryParams.name" clearable placeholder="请输入患者名称" @keyup.enter="handleQuery"/>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="handleQuery">
                    <template #icon>
                        <i-ep-search/>
                    </template>
                    搜索
                </el-button>
            </el-form-item>
        </el-form>
        <el-table v-adaptive="600" :border="true" :data="patVisitList" :header-cell-style="$headerCellStyle"
                  :stripe="true"
                  highlight-current-row @row-click="patVisitTableClick">
            <el-table-column :align="'center'" label="住院号" prop="hospitalNo" width="150px"/>
            <el-table-column :align="'center'" :show-overflow-tooltip="true" label="患者姓名" prop="name"
                             width="140px"/>
            <el-table-column :align="'center'" :show-overflow-tooltip="true" label="床号" prop="bedNo" width="60px"/>
            <el-table-column :align="'center'" :show-overflow-tooltip="true" label="性别" prop="sex" width="90">
                <template #default="scope">
                    <dict-data-tag :options="dictDataMap['sys_user_sex']" :value="scope.row.sex"/>
                </template>
            </el-table-column>
            <el-table-column :align="'center'" label="年龄" prop="age" width="220px">
                <template #default="scope">
                    {{ scope.row.age }} 岁
                    {{ scope.row.ageMonth }} 月
                    {{ scope.row.ageDay }} 天
                    {{ scope.row.ageHours }} 时
                    {{ scope.row.ageMinutes }} 分
                </template>
            </el-table-column>
            <el-table-column :align="'center'" label="身份证号" prop="idNo" width="200px"/>
            <el-table-column :align="'center'" label="电话" prop="phone" width="150px"/>
            <el-table-column :align="'center'" :show-overflow-tooltip="true" label="费别" prop="chargeType"
                             width="110px">
                <template #default="scope">
                    <dict-data-tag :options="dictDataMap['medical_insurance_type']" :value="scope.row.chargeType"/>
                </template>
            </el-table-column>
            <el-table-column :align="'center'" label="住院次数" prop="hospitalizationNumber" width="82"/>
        </el-table>
        <pagination v-model:limit="queryParams.pageSize" v-model:page="queryParams.pageNum" :total="total"
                    @pagination="getList"/>
    </div>
</template>
<script lang="ts" setup>
import {selectPagePatVisit, updatePatVisit} from "@/api/hospitalization/patVisit";
import {useUserStoreHook} from "@/store/modules/user";
import {useDictStoreHook} from "@/store/modules/dict";
import {getOrgVsUserList} from '@/api/system/user';
import {listSysOrgDict} from "@/api/system/sysOrgDict";

const emits = defineEmits(["transferToTheDeptOk"])
const useUserStore = useUserStoreHook();
// 登录用户信息
const userInfo = <UserInfoBusi>useUserStore.user.userInfo;
const dictStore = useDictStoreHook();
let dictDataMap = ref(dictStore.isTypeGetData('discharge_disposition_dict', 'patient_class_dict', 'sys_yes_no', 'admission_situation_dict',
    'entry_and_exit_hospital_status', 'medical_insurance_type', 'sys_user_sex'
))
// 护理字典集合
const nurseClassList = ref<any>([])
// 选中可是下用户list
const orgVsUserList = ref<any>([])
const form = ref<any>({})
// 床位信息
const bedInfo = ref<any>({})
const formRef = ref()
const patVisitList = ref<any>([])
const total = ref(0);
const {proxy} = getCurrentInstance() as any;
const currentSelectDept = proxy.$currentSelectDept
const date = proxy.$parseTime(new Date(), "{y}-{m}-{d} ");
const dateRange = ref([date + "00:00:00", date + "23:59:59"]);
const queryParams = ref<any>({
    pageNum: 1,
    pageSize: 10,
    orgId: userInfo.orgId,
    hospitalNo: "",
    wardFlag: "Y",
    deptFlag: "N"
})

// 表单校验
const rules = ref({
    doctorInCharge: [
        {required: true, message: "管床医师不能为空", trigger: "change"}
    ],
    upperDoctor: [
        {required: true, message: "上级医师不能为空", trigger: "change"}
    ],
    directorDoctor: [
        {required: true, message: "主任医生不能为空", trigger: "change"}
    ],
    qualityDoctor: [
        {required: true, message: "质控医生不能为空", trigger: "change"}
    ]
})

function openTransferToTheDept() {
    getList()
}

function submitForm() {
    (formRef.value as any).validate((valid) => {
        if (valid) {
            ElMessageBox.confirm("确定要将此患者移入科室吗？", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            }).then(() => {
                const formData = {
                    visitId: form.value.visitId,
                    doctorInCharge: form.value.doctorInCharge,
                    upperDoctor: form.value.upperDoctor,
                    directorDoctor: form.value.directorDoctor,
                    qualityDoctor: form.value.qualityDoctor,
                    isTransferToTheDept: "1",
                    deptAdmissionTo: form.value.deptAdmissionTo,
                }
                updatePatVisit(formData).then(() => {
                    ElMessage.success("患者移入病区成功")

                    emits("transferToTheDeptOk")
                })

            }).catch(() => {

            });
        }
    });
}

function empty() {
    patVisitList.value = []
    form.value = {}
    dateRange.value = [date + "00:00:00", date + "23:59:59"]
}

function handleQuery() {
    getList()
}

/** 查询患者住院信息列表 */
function getList() {
    selectPagePatVisit(proxy.$addDateRange(queryParams.value, dateRange.value)).then((response: any) => {
        patVisitList.value = response.rows;
        total.value = response.total;
    });
}


function patVisitTableClick(row: any) {
    getOrgVsUserList({deptId: currentSelectDept}).then((res: resType) => {
        orgVsUserList.value = res.data
        form.value = row
    })
}

/** 查询机构字典（护理等级） */
function listSysOrgDictFun() {
    listSysOrgDict({orgId: userInfo.orgId, type: "nurse_class"}).then((res: resType) => {
        nurseClassList.value = res.data
    })
}
listSysOrgDictFun()
defineExpose({
    openTransferToTheDept,
    empty
})
</script>

<style lang="scss" scoped>
:deep(.el-divider--horizontal) {
    margin: 10px 0;
}
</style>
